#include<cstdint>

namespace LccStuffCollector{
namespace TTFParser{


// item kind
/**
 * pure value
 * offset/index/addrea
 * 
 * */
// 1.999 (0x7fff) and -2.0 (0x8000). 
// 1.0 is stored as 16384 (0x4000) and -1.0 is stored as -16384 (0xc000).
// 01 11 1111 1111 1111  1.99  1*2^0+1*2^-1...
// 10 00 0000 0000 0000 -2.0
// 01 00 0000 0000 0000  1.0
// 11 00 0000 0000 0000 -1.0

typedef int16_t shortFrac;// 16-bit signed fraction; A shortFrac is an int16_t with a bias of 14. This means it can represent numbers between 1.999 (0x7fff) and -2.0 (0x8000). 1.0 is stored as 16384 (0x4000) and -1.0 is stored as -16384 (0xc000).
typedef int32_t Fixed;// 16.16-bit signed fixed-point number
typedef int16_t FWord;// 16-bit signed integer that describes a quantity in FUnits, the smallest measurable distance in em space.
typedef uint16_t uFWord;// 16-bit unsigned integer that describes a quantity in FUnits, the smallest measurable distance in em space.
typedef int16_t F2Dot14;// 16-bit signed fixed number with the low 14 bits representing fraction.
typedef int64_t longDateTime;// The long internal format of a date in seconds since 12:00 midnight, January 1, 1904. It is represented as a signed 64-bit integer.




// done
struct fileHeadCover{
#pragma pack(2)
	uint8_t scaler_type[4];//	A tag to indicate the OFA scaler to be used to rasterize this font; see the note on the scaler type below for more information.
	uint16_t numTables;//	number of tables
	uint16_t searchRange;//	(maximum power of 2 <= numTables)*16
	uint16_t entrySelector;//	log2(maximum power of 2 <= numTables)
	uint16_t rangeShift;//	numTables*16-searchRange
#pragma pack()
};




// done
struct tableItemCover{
#pragma pack(2)
	uint8_t tag[4];//	4-byte identifier
	uint32_t checkSum;//	checksum for this table
	uint32_t offset;//	offset from beginning of sfnt
	uint32_t length;//	length of this table in byte (actual length not padded length)
#pragma pack()
};




// done
// name post head maxp cmap loca glyf hhea hmtx
struct nameTableCover{
#pragma pack(2)
	uint16_t format;// Format selector. Set to 0.
	uint16_t count;// The number of nameRecords in this name table.
	uint16_t stringOffset;// Offset in bytes to the beginning of the name character strings.
	struct NameRecord{
		uint16_t platformID;// Platform identifier code.
		uint16_t platformSpecificID;// Platform-specific encoding identifier.
		uint16_t languageID;// Language identifier.
		uint16_t nameID;// Name identifier.
		uint16_t length;// Name string length in bytes.
		uint16_t offset;// Name string offset in bytes from stringOffset.
	}nameRecord[0];// The name records array. //  as NameRecord nameRecord[count];
	// [follow array] variable name;// Character strings. The character strings of the names. Note that these are not necessarily ASCII!
#pragma pack()
};




// struct postTable{};// now needn't




// wait test
struct headTableCover{
#pragma pack(2)
	Fixed version;// 0x00010000 if (version 1.0)
	Fixed fontRevision;// set by font manufacturer
	uint32_t checkSumAdjustment;// To compute: set it to 0, calculate the checksum for the 'head' table and put it in the table directory, sum the entire font as a uint32_t, then store 0xB1B0AFBA - sum. (The checksum for the 'head' table will be wrong as a result. That is OK; do not reset it.)
	uint32_t magicNumber;// set to 0x5F0F3CF5
	uint16_t flags;
	// bit 0 - y value of 0 specifies baseline
	// bit 1 - x position of left most black bit is LSB
	// bit 2 - scaled point size and actual point size will differ (i.e. 24 point glyph differs from 12 point glyph scaled by factor of 2)
	// bit 3 - use integer scaling instead of fractional
	// bit 4 - (used by the Microsoft implementation of the TrueType scaler)
	// bit 5 - This bit should be set in fonts that are intended to e laid out vertically, and in which the glyphs have been drawn such that an x-coordinate of 0 corresponds to the desired vertical baseline.
	// bit 6 - This bit must be set to zero.
	// bit 7 - This bit should be set if the font requires layout for correct linguistic rendering (e.g. Arabic fonts).
	// bit 8 - This bit should be set for an AAT font which has one or more metamorphosis effects designated as happening by default.
	// bit 9 - This bit should be set if the font contains any strong right-to-left glyphs.
	// bit 10 - This bit should be set if the font contains Indic-style rearrangement effects.
	// bits 11-13 - Defined by Adobe.
	// bit 14 - This bit should be set if the glyphs in the font are simply generic symbols for code point ranges, such as for a last resort font.
	uint16_t unitsPerEm;// range from 64 to 16384
	longDateTime created;// international date
	longDateTime modified;// international date
	FWord xMin;// for all glyph bounding boxes
	FWord yMin;// for all glyph bounding boxes
	FWord xMax;// for all glyph bounding boxes
	FWord yMax;// for all glyph bounding boxes
	uint16_t macStyle;
	// bit 0 bold
	// bit 1 italic
	// bit 2 underline
	// bit 3 outline
	// bit 4 shadow
	// bit 5 condensed (narrow)
	// bit 6 extended
	uint16_t lowestRecPPEM;// smallest readable size in pixels
	int16_t fontDirectionHint;
	// 0 Mixed directional glyphs
	// 1 Only strongly left to right glyphs
	// 2 Like 1 but also contains neutrals
	// -1 Only strongly right to left glyphs
	// -2 Like -1 but also contains neutrals
	int16_t indexToLocFormat;// 0 for short offsets, 1 for long
	int16_t glyphDataFormat;// 0 for current format
#pragma pack()
};




// wait test
struct maxpTableCover{
#pragma pack(2)
	Fixed version;// 0x00010000 (1.0)
	uint16_t numGlyphs;// the number of glyphs in the font
	uint16_t maxPoints;// points in non-compound glyph
	uint16_t maxContours;// contours in non-compound glyph
	uint16_t maxComponentPoints;// points in compound glyph
	uint16_t maxComponentContours;// contours in compound glyph
	uint16_t maxZones;// set to 2
	uint16_t maxTwilightPoints;// points used in Twilight Zone (Z0)
	uint16_t maxStorage;// number of Storage Area locations
	uint16_t maxFunctionDefs;// number of FDEFs
	uint16_t maxInstructionDefs;// number of IDEFs
	uint16_t maxStackElements;// maximum stack depth
	uint16_t maxSizeOfInstructions;// byte count for glyph instructions
	uint16_t maxComponentElements;// number of glyphs referenced at top level
	uint16_t maxComponentDepth;// levels of recursion, set to 0 if font has only simple glyphs
#pragma pack()
};




struct cmapSubTableCover{
#pragma pack(2)
	uint16_t format;// Set to 2
	uint16_t length;// Total table length in bytes
	uint16_t language;// Language code (see above)
	struct format0{};// Macintosh
	struct format2{
		uint16_t subHeaderKeys[256];// Array that maps high bytes to subHeaders: value is index * 8
		struct subHeader
		{	uint16_t firstCode;
			uint16_t entryCount;
			int16_t idDelta;
			uint16_t idRangeOffset;
		};
		subHeader subHeaders[0];// Variable length array of subHeader structures
		// [follow array] uint16_t glyphIndexArray[variable];// Variable length array containing subarrays
	};// Japanese, Chinese and Korean
	struct format4{
		uint16_t segCountX2;// 2 * segCount
		uint16_t searchRange;// 2 * (2**FLOOR(log2(segCount)))
		uint16_t entrySelector;// log2(searchRange/2)
		uint16_t rangeShift;// (2 * segCount) - searchRange
		uint16_t endCode[0];// Ending character code for each segment, last = 0xFFFF.
		// [follow array] uint16_t reservedPad;// This value should be zero
		// [follow array] uint16_t startCode[segCount];// Starting character code for each segment
		// [follow array] uint16_t idDelta[segCount];// Delta for all character codes in segment
		// [follow array] uint16_t idRangeOffset[segCount];// Offset in bytes to glyph indexArray, or 0
		// [follow array] uint16_t glyphIndexArray[variable];// Glyph index array
	};// 16 bit mappings
	struct format6{};// dense 16 bit mappings
	struct format8{};// mixed 16/32-bit and pure 32-bit mappings. This supports text encoded with surrogates in Unicode 2.0 and later.
	struct format10{};// same as format 8
	struct format12{};// same as format 8
	struct format13{};// same as format 8
	struct format14{};// same as format 8
	union{format0 f0;
	      format2 f2;
	      format4 f4;
	      format6 f6;
	      format8 f8;
	      format10 f10;
	      format12 f12;
	      format13 f13;
	      format14 f14;
	  };
#pragma pack()
};

// done main, format yet not
struct cmapTableCover{
#pragma pack(2)
	uint16_t version;// Version number (Set to zero)
	uint16_t numberSubtables;// Number of encoding subtables
	struct SubTable{
		uint16_t platformID;// Platform identifier
		uint16_t platformSpecificID;// Platform-specific encoding identifier
		uint32_t offset;// Offset of the mapping table
	}subTable[0];
#pragma pack()
};





// done
// FILE STRUCTURE: T might be uint16 or uint32 which decide by 'head' table's indexToLocFormat item
// FILE STRUCTURE: The number of glyphs in a font must be stored in the Maximum Profile 'maxp' table.// The actual local offset (divided by 2 if is uint16) is stored. The value of n is the number of glyphs in the font + 1. The number of glyphs in the font is found in the maximum profile table.
struct locaTableCover{
#pragma pack(2)
	union{uint16_t offsets_16[0];uint32_t offsets_32[0];};
#pragma pack()
};




// done
struct glyfItemCover{
#pragma pack(2)
	int16_t numberOfContours;
	// If the number of contours is positive or zero, it is a single glyph;
	// If the number of contours less than zero, the glyph is compound
	FWord xMin;// Minimum x for coordinate data
	FWord yMin;// Minimum y for coordinate data
	FWord xMax;// Maximum x for coordinate data
	FWord yMax;// Maximum y for coordinate data// Fword can be any type?
	struct simpleCase{
		uint16_t endPtsOfContours[0/*n*/];// Array of last points of each contour; n is the number of contours; array entries are point indices
		// [follow array] uint16_t instructionLength;// Total number of bytes needed for instructions
		// [follow array] uint8_t instructions[instructionLength];// Array of instructions for this glyph
		// [follow array] uint8_t flags[variable]// Array of flags
		// [follow array] uint8_t or int16_t xCoordinates[];// Array of x-coordinates; the first is relative to (0,0), others are relative to previous point
		// [follow array] uint8_t or int16_t yCoordinates[];// Array of y-coordinates; the first is relative to (0,0), others are relative to previous point
	};
	struct componentCase{
		// uint16_t flags;// Component flag
		// uint16_t glyphIndex;// Glyph index of component
		// int16_t, uint16_t, int8_t or uint8_t argument1;// X-offset for component or point number; type depends on bits 0 and 1 in component flags
		// int16_t, uint16_t, int8_t or uint8_t argument2;// Y-offset for component or point number type depends on bits 0 and 1 in component flags
		// [follow union] transformation option;// One of the transformation options from Table 19
	};
	union{simpleCase simple;componentCase component;};
#pragma pack()
};




// struct hheaTable{};
// struct hmtxTable{};

}// namespace TTFParser
}// namespace LccStuffCollector